#include "toolswidget.h"
#include <QTableWidget>
#include <QToolButton>
#include <QGridLayout>
#include <QAction>

#include <Tool>
#include <ToolCategory>


ToolsWidget::ToolsWidget(QWidget *parent) :
    QWidget(parent)
{
    m_pTabWidget = new QTabWidget(this);
    //m_pTabWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setMinimumHeight(100);
}

ToolsWidget::~ToolsWidget()
{

}

void ToolsWidget::addCategoryTab(QPointer<ToolCategory> category)
{
    QWidget* pTabWidget = new QWidget(this);
    pTabWidget->setLayout(new QGridLayout);
    int tabID = m_pTabWidget->addTab(pTabWidget, category->icon(), "");
    m_pTabWidget->setTabToolTip(tabID, category->name());
}
void ToolsWidget::addToolButton(QPointer<QAction> action, const QString& categoryName)
{
    int tabIndex = -1;

    for(int i=0;i<m_pTabWidget->count();i++)
    {
        if(m_pTabWidget->tabToolTip(i) == categoryName)
        {
            tabIndex = i;
            break;
        }
    }

    Q_ASSERT(tabIndex != -1);

    QGridLayout* pLayout = static_cast<QGridLayout*>(m_pTabWidget->widget(tabIndex)->layout());
    Q_ASSERT(pLayout);

    int count = pLayout->count();

    int column = (count % 2);
    int row = count/2;

    QToolButton* pToolButton = new QToolButton(this);
    //pToolButton->resize(32, 32);
    pToolButton->setDefaultAction(action);

    pLayout->addWidget(pToolButton, row, column, Qt::AlignLeft | Qt::AlignTop);
}
